/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.service;

import com.ivanbiz.dao.PengajarDAO;
import com.ivanbiz.dao.impl.PengajarDAOImpl;
import com.ivanbiz.model.Pengajar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

/**
 *
 * @author iw20149x
 */
public class PengajarService extends SelectorComposer<Component> {

    PengajarDAO pengajarDAO;
    @Wire
    Listbox listPengajar;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        pengajarDAO = new PengajarDAOImpl();
        loadPengajar();
    }

    public void loadPengajar() {
        try {
            listPengajar.getItems().clear();
            int no = 0;
            int x = 0;
            List<Pengajar> loadPengajar = pengajarDAO.getAll(Pengajar.class);
            for (Pengajar pengajar : loadPengajar) {
                no += 1;
                Listitem item = new Listitem();
                item.setValue(pengajar);
                item.appendChild(new Listcell(String.valueOf(no)));
                item.appendChild(new Listcell(pengajar.getNIP()));
                item.appendChild(new Listcell(pengajar.getNama()));
                item.appendChild(new Listcell(pengajar.getHandphone()));
                item.appendChild(new Listcell(pengajar.getEmail()));
                item.appendChild(new Listcell(pengajar.getAlamat()));
                listPengajar.appendChild(item);
                ++x;
            }
        } catch (Exception ex) {
            Logger.getLogger(PengajarService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Listen("onClick=#tambah;")
    public void tambah() {
        Window win = (Window) Executions.createComponents("/pengajarAction.zul", null, null);
        win.doModal();
        loadPengajar();
    }

    @Listen("onClick=#ubah;")
    public void ubah() {
        if (listPengajar.getSelectedItem() == null) {
            Messagebox.show("Pilih data yang akan di ubah");
        } else {
            Pengajar pengajar = listPengajar.getSelectedItem().getValue();
            Map map = new HashMap();
            map.put("pengajar", pengajar);
            Window win = (Window) Executions.createComponents("/pengajarAction.zul", null, map);
            win.setTitle("Ubah Pengajar");
            win.doModal();
            loadPengajar();
        }
    }

    @Listen("onClick=#hapus;")
    public void hapus() throws Exception {
        if (listPengajar.getSelectedItem() == null) {
            Messagebox.show("Pilih data yang akan di ubah");
        } else {
            Pengajar pengajar = listPengajar.getSelectedItem().getValue();
            pengajarDAO.delete(pengajar);
            loadPengajar();
        }
    }
}
